package ar.edu.sovietrussia.customcomponents.personconfig;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.user.PersonService;
import users.Person;
import ar.edu.sovietrussia.customcomponents.util.InwardReplaceablePanel;

/**
 * Component for Person personal configuration
 */

@AuthorizeInstantiation("ADMIN")
public class PersonalUserConfiguration extends InwardReplaceablePanel {

    private static final long serialVersionUID = 723515346606074629L;

    @SpringBean(name = "service.person")
    private PersonService personService;

    @SuppressWarnings("unchecked")
    public PersonalUserConfiguration(final String id, final Class<?> clazz) {
        super(id);

        final Person aModel = (Person) this.getSession().getAttribute("model");

        Form<String> form = new Form<String>("personalConfigForm");

        form.add(new TextField("name", new PropertyModel(aModel, "name")));

        form.add(new TextField("surname", new PropertyModel(aModel, "surname")));

        form.add(new TextField("mail", new PropertyModel(aModel, "mail")));

        form.add(new TextField("userID", new PropertyModel(aModel.getUserAccount(), "userID")));

        form.add(new TextField("password", new PropertyModel(aModel.getUserAccount(), "password")));

        // Accept Button
        form.add(new Button("accept_button") {
            private static final long serialVersionUID = 5119888790644143879L;

            @Override
            public void onSubmit() {
                PersonalUserConfiguration.this.personService.updatePerson(aModel);
                PersonalUserConfiguration.this.goBack(clazz);
            }

        });

        // Go Back Button
        form.add(new Button("back_button") {
            private static final long serialVersionUID = 5119888790644143879L;

            @Override
            public void onSubmit() {
                PersonalUserConfiguration.this.goBack(clazz);
            }
        });

        this.add(form);
    }

    public void setPersonService(final PersonService personService) {
        this.personService = personService;
    }

    public PersonService getPersonService() {
        return this.personService;
    }
}
